using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Coderoom.ScrumPlanner.Utilities;
using Xunit;

namespace Coderoom.ScrumPlanner.Tests.Unit.UtilityTests
{
	public class XmlExtensionTests
	{
		[Fact]
		public void ImmediateDescendants_returns_IEnumerable()
		{
			// arrange
			var data = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new XElement("Data", new XElement("Test"), new XElement("Test")));

			// act
			var result = data.ImmediateDescendants("Test");

			// assert
			Assert.IsAssignableFrom<IEnumerable<XElement>>(result);
			Assert.Equal(2, result.ToArray().Length);
		}

		[Fact]
		public void HasAttribute_returns_true_if_attribute_is_found_and_contains_value()
		{
			// arrange
			var data = new XElement("Test", new XAttribute("attr", "val"));

			// act
			var result = data.HasAttribute("attr");

			// assert
			Assert.True(result);
		}

		[Fact]
		public void HasAttribute_returns_false_if_attribute_is_found_and_contains_no_value()
		{
			// arrange
			var data = new XElement("Test", new XAttribute("attr", ""));

			// act
			var result = data.HasAttribute("attr");

			// assert
			Assert.False(result);
		}

		[Fact]
		public void HasAttribute_returns_false_if_attribute_is_not_found()
		{
			// arrange
			var data = new XElement("Test", new XAttribute("attr", "val"));

			// act
			var result = data.HasAttribute("attr2");

			// assert
			Assert.False(result);
		}

		[Fact]
		public void ReadAttribute_returns_string()
		{
			// arrange
			var data = new XElement("Test", new XAttribute("attr", "val"));

			// act
			var result = data.ReadAttribute("attr");

			// assert
			Assert.Equal("val", result);
		}

		[Fact]
		public void ReadAttributeAsGuid_returns_Guid()
		{
			// arrange
			var data = new XElement("Test", new XAttribute("attr", "83FAF461-473B-4CAB-A845-6AF74C8B8489"));

			// act
			var result = data.ReadAttributeAsGuid("attr");

			// assert
			Assert.Equal(new Guid("83FAF461-473B-4CAB-A845-6AF74C8B8489"), result);
		}

		[Fact]
		public void ReadChildValue_returns_string()
		{
			// arrange
			var data = new XElement("Test", new XElement("child", "val"));

			// act
			var result = data.ReadChildValue("child");

			// assert
			Assert.Equal("val", result);
		}
	}
}